% Copyright (C) 2014-19 Benjamin Born and Johannes Pfeifer 
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

fid_g=fopen(['Results/Descriptives_',impulse_name,'.txt'],'w+');
topstring_descriptives={'\begin{table}[t!]'
'  \centering'
'  \caption{Sample}'
'   \tabcolsep=.2in'
'\def \arraystretch{.9}'
'    \begin{tabular}{lccrr}'
'    \toprule'
'    Country   & first obs & last obs & T without NaN & NAN Obs \\'
'    \midrule'};

for ii=1:size(topstring_descriptives,1)
fprintf(fid_g,'%-150s \n',topstring_descriptives{ii,1});
end
for country_iter=1:size(country_indices_non_NaN)
    country_name=country_indicator_names_mapping{data_array_for_regression_stacked_by_variable(1,country_indices_non_NaN(country_iter),pos.country_ident)};
    stacked_data=squeeze(cat(3,dependent_variable_for_run(:,country_iter),data_array_for_regression_for_run(:,country_iter,:)));
    first_obs_index=find(~any(isnan(stacked_data),2),1,'first');
    last_obs_index=find(~any(isnan(stacked_data),2),1,'last');
    NaN_obs=sum(any(isnan(stacked_data(first_obs_index:last_obs_index,:)),2));
    first_obs=data_array_for_regression_stacked_by_variable(time_indices_non_NaN(first_obs_index),pos.timeline);
    last_obs=data_array_for_regression_stacked_by_variable(time_indices_non_NaN(last_obs_index),pos.timeline);
    fprintf(fid_g,'%-30s \t & \t %6s \t & \t %6s \t & \t %3u \t & \t %3u \\\\ \n',...
    country_name,...
    [num2str(floor(first_obs)),'Q',num2str(mod(first_obs,1)/0.25+1)],[num2str(floor(last_obs)),'Q',num2str(mod(last_obs,1)/0.25+1)],...
    last_obs_index-first_obs_index+1-NaN_obs,NaN_obs);
    country_info_cell{country_iter,1}=country_name;
    country_info_cell{country_iter,2}=[num2str(floor(first_obs)),'Q',num2str(mod(first_obs,1)/0.25+1)];
    country_info_cell{country_iter,3}=[num2str(floor(last_obs)),'Q',num2str(mod(last_obs,1)/0.25+1)];
    country_info_cell{country_iter,4}=last_obs_index-first_obs_index+1-NaN_obs;
    country_info_cell{country_iter,5}=NaN_obs;
    country_info_cell{country_iter,6}=first_obs;
end
save(['Results/Descriptives_',impulse_name],'country_info_cell')

bottomstring_descriptives={'    \bottomrule'
'    \end{tabular}'
'\caption*{\footnotesize{\emph{Notes:} }}'
'  \label{tab:descriptives_FC}'
'\end{table}'};

for ii=1:size(bottomstring_descriptives,1)
fprintf(fid_g,'%-150s \n',bottomstring_descriptives{ii,1});
end
